package it.gameplanet.client.widgets.articolo;

import it.gameplanet.client.GamePlanetContext;
import it.gameplanet.client.utils.AbstractAsyncCallback;
import it.gameplanet.client.widgets.support.Dialogs;
import it.gameplanet.model.Articolo;
import it.gameplanet.service.GamePlanetService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AcquistaArticoloDialog extends DialogBox {

	private Articolo articolo;

	final Button inserisciButton = new Button("OK");
	final Button annullaButton = new Button("Annulla");

	final Label nomeArticolo = new Label();
	final Label dataRichiesta = new Label();
	
	public AcquistaArticoloDialog(Command callback) {
		super();
		
		initDialog(callback);
	}

	private void initDialog(final Command callback) {

		this.setText("Acquisto articolo selezionato");
		this.setAnimationEnabled(true);
		VerticalPanel dialogVPanel = new VerticalPanel();
		
		dialogVPanel.add(new HTML("<b>Titolo:</b>"));
		dialogVPanel.add(nomeArticolo);
		dialogVPanel.add(new HTML("<b>Data Richiesta:</b>"));
		dialogVPanel.add(dataRichiesta);

		dialogVPanel.add(new HTML("<b>Nome:</b>"));
		final TextBox nome = new TextBox();
		nome.setName("Nome");
		dialogVPanel.add(nome);
		
		dialogVPanel.add(new HTML("<b>Cognome:</b>"));		
		final TextBox cognome = new TextBox();
		cognome.setName("Cognome");
		dialogVPanel.add(cognome);

		dialogVPanel.add(new HTML("<b>Email:</b>"));		
		final TextBox email = new TextBox();
		email.setName("Email");
		email.setText(GamePlanetContext.getUser()!=null?GamePlanetContext.getUser().getEmail():"<inserisci_qui_la_tua_email>");
		dialogVPanel.add(email);
		
	    HorizontalPanel bottoniera = new HorizontalPanel();
	    bottoniera.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    
		bottoniera.add(inserisciButton);
		
		annullaButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});		
		bottoniera.add(annullaButton);

		inserisciButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				List<String> errors = new ArrayList<String>();
				
				inserisciButton.setEnabled(false);
				annullaButton.setEnabled(false);
				
				if( isValidEmail(email, errors) && isNotEmpty(nome, errors) && isNotEmpty(cognome, errors)) {
					GamePlanetService.Utils.getGamePlanetServiceAsync().richiediAcquisto(articolo, email.getText(), nome.getText(), cognome.getText(), new AbstractAsyncCallback<Void>() {

						@Override
						public void onSuccess(Void result) {
							Dialogs.showMessage("Richiesta inoltrata con successo per "+email.getText());
							callback.execute();
						}
						
					});					
				} else {
					StringBuffer sb = new StringBuffer("<p>Attenzione, i dati non sono corretti</p>");
					
					for(String errore: errors) {
						sb.append(errore);
						sb.append("<br>");
					}
					
					Dialogs.showMessage(sb.toString());
				}
				
			}

		});		
		
		dialogVPanel.add(bottoniera);

	    this.setWidget(dialogVPanel);
	}
	
	public void editNews(Articolo newsP) {
		
		this.articolo = newsP;
		inserisciButton.setEnabled(true);
		annullaButton.setEnabled(true);
		
		syncNews();
		
		show();
		center();
	}

	private void syncNews() {
		
		nomeArticolo.setText(articolo.getNome());
		dataRichiesta.setText(DateTimeFormat.getShortDateFormat().format(new Date()));
	}
	
	private boolean isNotEmpty(TextBox nome, List<String> errors) {
		boolean b = nome.getText()!=null && !"".equals(nome.getText());
		
		if(!b) {
			errors.add("<b>"+nome.getName()+"</b> non puo' essere vuoto");
		}
		
		return b;
	}

	private boolean isValidEmail(TextBox email, List<String> errors) {
		boolean b = isNotEmpty(email, errors) && email.getText().lastIndexOf('@')!=-1 && email.getText().lastIndexOf('.')!=-1;

		if(!b) {
			errors.add("<b>"+email.getName()+"</b> non e' corretta.");
		}
		
		return b;
	}
	
}
